//
//  CharacterViewController.swift
//  SkylowAPP
//
//  Created by 王王凤丽 on 2024/9/20.
//

import UIKit

let kScreenWidth = UIScreen.main.bounds.width
let kScreenHeight = UIScreen.main.bounds.height

class CharacterViewController: UIViewController {

    let selectChatView = UIView()
    
    @IBOutlet weak var characterCollView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        initWithUI()
    }
   func initWithUI(){
       view.backgroundColor = UIColor.colorWith(hexString: "#F2F3F5")
       self.navigationItem.title = "Skylow"
       
       self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_nav_search"), style: .plain, target: self, action: #selector(leftBtnClick))
       
       self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_nav_more"), style: .plain, target: self, action: #selector(rightBtnClick))
       
       characterCollView.register(UINib.init(nibName: "CharCollectionCell", bundle: nil), forCellWithReuseIdentifier: "CharCollectionCell")
       characterCollView.delegate = self
       characterCollView.dataSource = self
       characterCollView.backgroundColor = UIColor.colorWith(hexString: "#333333")
       characterCollView.reloadData()
    }
    
    @objc func leftBtnClick(){
        let searchVC = SearchCharViewController()
        self.navigationController?.pushViewController(searchVC, animated: true)
    }
    
    @objc func rightBtnClick(){
        
    }
    // MARK: --- 添加
    @objc func addSelectChatButton() {
        selectChatView.backgroundColor = UIColor.colorWith(hexString: "#272727")
        selectChatView.addRoundedCorners(withRadius: 4)
        self.view.addSubview(selectChatView)
        
        
        let closeButton = UIButton()
        closeButton.setImage(UIImage(named: "character_close_icon"), for: .normal)
        closeButton.addTarget(self, action: #selector(clickPingMuShow), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(closeButton)
        closeButton.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        let newChatButton = UIButton()
        newChatButton.setTitle("Chat in a new world", for: .normal)
        newChatButton.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        newChatButton.setTitleColor(.black, for: .normal)
//        newChatButton.titleLabel?.font = UIFont(name: "Inter", size: 12)
        newChatButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
//        newChatButton.addTarget(self, action: #selector(clickNewChatButton), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(newChatButton)
        newChatButton.snp.makeConstraints { (make) in
            make.top.equalTo(closeButton.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(34)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        selectChatView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(newChatButton.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(1)
        }
        
        let worldChatButton = UIButton()
        worldChatButton.setTitle("Chat in world sbaibxiad...", for: .normal)
        worldChatButton.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        worldChatButton.setTitleColor(.black, for: .normal)
//        worldChatButton.titleLabel?.font = UIFont(name: "Inter", size: 12)
        worldChatButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)

//        newChatButton.addTarget(self, action: #selector(clickNewChatButton), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(worldChatButton)
        worldChatButton.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(34)
        }
        
        selectChatView.snp.makeConstraints { (make) in
            make.width.equalTo(217)
//            make.height.equalTo(285)
            make.bottom.equalTo(worldChatButton.snp.bottom).offset(40)
            make.center.equalToSuperview() // view1的中心和父视图一样
        }
    }
    
    
    // MARK: --- 关闭
    @objc func clickPingMuShow() {
       // 确保布局已经完成，然后删除子视图
        selectChatView.removeFromSuperview()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
//MARK:- collectionView的数据源和代理方法
extension CharacterViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    // MARK: --- 区
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    // MARK: --- 行
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 17
    }
    // MARK: --- 行间距，一个区之间的
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 14
    }
    // MARK: --- item 宽高
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemWidth = (kScreenWidth - 14 * 2 - 10) / 2
        return CGSize(width: itemWidth,height: 354)
    }
    // MARK: --- head的宽高
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: kScreenWidth,height:14)
    }

    // MARK: --- 区块的设定
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
    }
    // MARK: --- cellForItemAt indexPath
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CharCollectionCell", for: indexPath) as! CharCollectionCell
        cell.chatButton.addTarget(self, action: #selector(addSelectChatButton), for: UIControl.Event.touchUpInside)
        return cell
    }
    
    //MARK: - 点击cell
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let charDetails = CharacterDetailsViewController()
        self.navigationController?.pushViewController(charDetails, animated: true)
    }
}

